package com.zh.rent.comm;

import com.github.pagehelper.PageInfo;
import com.zh.rent.pojo.*;
import com.zh.rent.service.*;
import com.zh.rent.util.EmailUtil;
import com.zh.rent.util.ExpireTime;
import com.zh.rent.util.JsonResult;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.test.context.junit4.SpringRunner;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;


@Configuration
@EnableScheduling
public class TimingTask {

    @Autowired
    AccountService AccountServiceImpl;
    @Autowired
    InvitationService InvitationServiceImpl;
    @Autowired
    MailContentService MailContentServerImpl;
    @Autowired
    StanAccountService StanAccountServiceImpl;

    @Autowired
    SetupService SetupServiceImpl;

    @Scheduled(cron="0 30 0 * * ?")
    void task() {
            System.out.println("定时任务,------执行");
        Setup setups = SetupServiceImpl.setsetupAll();
        int logNum=0;
        try{
            logNum=Integer.parseInt(setups.getReserved1());
        }catch (NumberFormatException e){}
        int uplogres = InvitationServiceImpl.uplogres(logNum);
        if(uplogres>0){
            System.out.println("定时任务执行成功，所有邀请码天数减一，登录次数回3");
        }
        int i = MailContentServerImpl.delMail();
        if(i>0){
            System.out.println("定时任务执行成功，验证码清除");
        }
    }

    @Scheduled(cron="0 */2 * * * ?")
    void expire() {
        List<Invitation> list = InvitationServiceImpl.selInvall();
        for (Invitation invitation : list) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date();
            String format = sdf.format(date);
            Timestamp creationTime = (Timestamp) invitation.getUseTime();
            String ssql = creationTime.toString();
            //当前时间的时间戳
            String s = ExpireTime.dateToStamp(format);
            String s2 = ExpireTime.plusDay((Integer)invitation.getResidue(), ssql);
            //得到到期时间的时间戳
            String s1 = ExpireTime.dateToStamp(s2);
            //当前时间
            long format1=0;
            //到期时间
            long ssql2=0;
            try{
                format1=   Long.parseLong(s);
                ssql2=Long.parseLong(s1);
            }catch (Exception e){
                System.out.println("转换时间戳失败");
            }
            if(ssql2<format1){
                System.out.println("到期时间到了");
                System.out.println("到期邀请码："+invitation.getInvitationCode());
                int i = InvitationServiceImpl.delInvBccid(invitation.getInvitationid());
                if(i>0){
                    System.out.println("到期邀请码删除成功");
                }
            }
        }
    }


//    @Scheduled(cron="0 */1 * * * ?")
//    void Maill() {
//        List<Account> accounts = AccountServiceImpl.selAccAll();//查询所有的账号
//        Integer accid = null;
//        String accName = null;
//        String accType = null;
//        String MailboxName = null;
//        String Authorization = null;
//        String Host = null;
//
//        PageInfo<MailContent> mailContentPageInfo = null;
//        if (accounts != null) {
//            for (Account account : accounts) {
//                List<MailContent> list = new ArrayList<>();
//                if (account.getStandbyid() == null) {
//                    accid = account.getAccountid();//账号id
//                    accName = account.getAccountName();//账号
//                    accType = account.getAccountType();//类型
//                    MailboxName = account.getMailboxName();//邮箱
//                    Authorization = account.getAuthorization();//授权码
//                    Host = account.getHost();//服务器
//                } else {
//                    StandbyAccount standbyAccount = StanAccountServiceImpl.selStanAcc(account.getStandbyid());
//                    accid = standbyAccount.getStandbyid();//账号id
//                    accName = standbyAccount.getAccountName();//账号
//                    accType = standbyAccount.getAccountType();//类型
//                    MailboxName = standbyAccount.getMailboxName();//邮箱
//                    Authorization = standbyAccount.getAuthorization();//授权码
//                    Host = standbyAccount.getHost();//服务器
//                }
//                List<Map<String, Object>> emali = null;
//                try {
//                    emali = EmailUtil.Emali(MailboxName, Authorization, Host);
//                    //Future<String> stringFuture = asyncTaskService.doTaskTwo(MailboxName, Authorization, Host, accid, accName, accType);
//                    if (emali != null) {
//                        for (Map<String, Object> strMap : emali) {
//                            MailContent mailContent = new MailContent();
//                            mailContent.setAccountid(accid);
//                            mailContent.setAccountName(accName);
//                            mailContent.setAccountType(accType);
//                            mailContent.setMailboxName(MailboxName);
//                            mailContent.setMailTime((String) strMap.get("Ttime"));
//                            mailContent.setMaliText((String) strMap.get("txtcontent"));
//                            mailContent.setSite((String) strMap.get("site"));
//                            mailContent.setMalicode((String) strMap.get("code"));
//                            list.add(mailContent);
//                        }
//                    }
//                    System.out.println("获取获取11111");
//                    if(list!=null){
//                        int i = MailContentServerImpl.updateMail(list);//添加进数据库
//                    }
//                } catch (Exception e) {
//                    System.out.println("验证码存数据库出错了：" + e.getMessage());
//                    continue;
//                }
//            }
//        }
//    }

    //@Scheduled(cron="*/30 * * * * ?")
    void Maill1() {
        Setup setup = SetupServiceImpl.setsetupAll();
        List<MailContent> list = new ArrayList<>();
        List<Map<String, Object>> emali = null;
                try {
                    emali = EmailUtil.Emali(setup.getReserved2(), setup.getReserved3(), setup.getReserved4());
                    //Future<String> stringFuture = asyncTaskService.doTaskTwo(MailboxName, Authorization, Host, accid, accName, accType);
                    if (emali != null) {
                        for (Map<String, Object> strMap : emali) {
                            MailContent mailContent = new MailContent();
                          //  mailContent.setAccountid(accid);
                            mailContent.setAccountName((String) strMap.get("accName"));
                          //  mailContent.setAccountType(accType);
                            String s=(String) strMap.get("MailboxName");
                            String substring = s.substring(1, s.length()-1);
                            mailContent.setMailboxName(substring);
                            mailContent.setMailTime((String) strMap.get("Ttime"));
                            mailContent.setMaliText((String) strMap.get("txtcontent"));
                            mailContent.setSite((String) strMap.get("site"));
                            mailContent.setMalicode((String) strMap.get("code"));
                            System.out.println("获取获取3333:"+strMap.get("accName"));
                            list.add(mailContent);
                        }
                    }
                    System.out.println("获取获取11111:"+list.toString());

                    if(list!=null){
                        int i = MailContentServerImpl.updateMail(list);//添加进数据库
                    }
                } catch (Exception e) {
                    System.out.println("验证码存数据库出错了：" + e.getMessage());
                }
            }
}
